<?php
/**
 * Created by PhpStorm.
 * User: DMF
 * Date: 2017/4/11
 * Time: 15:13
 */

namespace Admin\Model;
use Think\Model;

class CommentsModel extends Model
{
//    设置心情日记数据
    public function setPersonalDiaryData($data,$id=null){
        if($id==null){
            $db = M('personaldiary');
            $result = $db->add($data);
            //成功增加数据返回ture
            if($result){
                return true;
            }else{
                return false;
            }
        }else{
            $db = M('personaldiary');
            $result = $db->where('id='.$id)->save($data);
            //var_dump($data);var_dump($id);die;
            //成功增加数据返回ture
            if($result){
                return true;
            }else{
                return false;
            }
        }

    }
//    获取心情日记数据
    public function getPersonalDiaryData($id=null){
        //是否要指定查找一个id的日记
        if($id==null){
            $db = M('personaldiary');
            $result = $db->order('id')->select();
            //获取到数据
            if($result){
                return $result;
            }else{
                return false;
            }
        }else{
            $db = M('personaldiary');
            $result = $db->where('id='.$id)->find();
            //获取到数据
            if($result){
                return $result;
            }else{
                return false;
            }
        }
    }
//    删除心情日记数据
    public function delPersonalDiaryData($id){
        $db = M('personaldiary');
        $result = $db->where('id='.I('id'))->delete();
        //成功删除数据返回ture
        if($result){
            return true;
        }else{
            return false;
        }
    }
//    获取留言数据
    public function getMessageData($id=null){

        if($id == null) {
            $db = M('comments');
            $obj = $db->where('type=1')->select();
            //var_dump($obj);
            return $obj;
        }else{
            $db = M('comments');
            $obj = $db->where('type=1 and id='.$id)->find();
            return $obj;
        }

    }
//    添加留言数据
    public function setMessageData($data,$id=null){
        $db = M('comments');
        $arr = array(
            "parent"=>$data['parent'],
            "belong"=>$data['belong'],
            "content"=>$data['content'],
            "time"=>$data['time'],
            "email"=>$data['email'],
            "username"=>$data['username'],
            "type"=>$data['type'],
        );
        if($id==null){
            $result = $db->add($arr);
            if($result){
                return true;
            }else{
                return false;
            }
        }else{
            $result = $db->where('id='.$id)->save($arr);
            if($result){
                return true;
            }else{
                return false;
            }
        }
    }

//    删除留言
    public function delMessageBoardData($id){

        $db = M('comments');
        $result = $db->where('id='.$id)->delete();
        if($result){
            return true;
        }else{
            return false;
        }
    }
//    获取表白信息
    public function getRewardData(){
        $db = M('reward');
        $list = $db->select();
        if($list){
            return $list;
        }else{
            return false;
        }

    }
//    设置表白信息
    public function setRewardData($data){
        $db = M('reward');
        if(session('username')!=null){
            $data['fromname'] = session('username');
        }
        $result = $db->add($data);
        if($result){
            return true;
        }else{
            return false;
        }
    }
//    删除表白信息
    public function delRewardData($id){
        $db = M('reward');
        $list = $db->where('id='.$id)->delete();
        if($list){
            return true;
        }else{
            return false;
        }
    }
}
